package handler

import (
	"context"
	"fmt"
	"lb_two-exam/doctor-srv/global"
	"lb_two-exam/doctor-srv/internal/model/dao"
	pb "lb_two-exam/doctor-srv/proto"
)

type DoctorService struct {
	pb.UnimplementedDoctorServer
}

func (c *DoctorService) DoctorRegister(ctx context.Context, req *pb.DoctorRegisterRequest) (resp *pb.DoctorRegisterResponse, err error) {

	//接受参数
	//入库
	doctor := dao.Doctor{
		Name:      req.Name,
		Img:       req.Img,
		School:    int(req.School),
		Room:      int(req.Room),
		Work:      int(req.Work),
		Status:    int(req.Status),
		Price:     float64(req.Price),
		LookPrice: float64(req.LookPrice),
		Intro:     req.Intro,
	}
	err = global.DB.Create(&doctor).Error
	if err != nil {
		return nil, fmt.Errorf("医生信息注册失败" + err.Error())
	}

	//err = server.SyncToEs(&doctor)
	//if err != nil {
	//	return nil, fmt.Errorf("es同步失败")
	//}
	return &pb.DoctorRegisterResponse{
		Message: "注册成功",
	}, nil
}
func (c *DoctorService) DoctorList(ctx context.Context, req *pb.DoctorListRequest) (resp *pb.DoctorListResponse, err error) {

	var doctor []dao.Doctor
	err = global.DB.Find(&doctor).Error
	if err != nil {
		return nil, err
	}
	var slice []*pb.DoctorMessage
	for _, d := range doctor {
		slice = append(slice, &pb.DoctorMessage{
			Name:      d.Name,
			Img:       d.Img,
			School:    int32(d.School),
			Room:      int32(d.Room),
			Work:      int32(d.Work),
			Status:    int32(d.Status),
			Price:     float32(d.Price),
			LookPrice: float32(d.LookPrice),
			Intro:     d.Intro,
		})
	}
	return &pb.DoctorListResponse{
		DoctorList: slice,
	}, nil
}

func (c *DoctorService) Register(ctx context.Context, req *pb.RegisterRequest) (resp *pb.RegisterResponse, err error) {

	message := dao.Message{
		Hospital: req.Hospital,
		Doctor:   req.Doctor,
		Address:  req.Address,
		Room:     req.Room,
		Status:   req.Status,
		Price:    float64(req.Price),
		Intro:    req.Intro,
		DoctorId: int(req.DoctorId),
		PersonId: int(req.PersonId),
	}
	err = global.DB.Create(&message).Error
	if err != nil {
		return nil, fmt.Errorf("预约失败" + err.Error())
	}
	return &pb.RegisterResponse{
		Message: "预约成功",
	}, nil
}
func (c *DoctorService) Show(ctx context.Context, req *pb.ShowRequest) (resp *pb.ShowResponse, err error) {

	var message []*dao.Message

	err = global.DB.Where("person_id = ?", req.PersonId).First(&message).Error
	if err != nil {
		return nil, fmt.Errorf("预约信息展示失败" + err.Error())
	}
	var slice []*pb.Message
	for _, d := range message {
		slice = append(slice, &pb.Message{
			DoctorId: int32(d.DoctorId),
			PersonId: int32(d.PersonId),
			Hospital: d.Hospital,
			Doctor:   d.Doctor,
			Address:  d.Address,
			Room:     d.Room,
			Status:   d.Status,
			Price:    float32(d.Price),
			Intro:    d.Intro,
		})
	}
	return &pb.ShowResponse{
		ShowList: slice,
	}, nil
}
func (c *DoctorService) Update(ctx context.Context, req *pb.UpdateRequest) (resp *pb.UpdateResponse, err error) {

	message := dao.Message{
		Hospital: req.Hospital,
		Doctor:   req.Doctor,
		Address:  req.Address,
		Room:     req.Room,
		Status:   req.Status,
		Price:    float64(req.Price),
		Intro:    req.Intro,
		DoctorId: int(req.DoctorId),
	}
	err = global.DB.Where("person_id = ?", req.PersonId).Save(&message).Error
	if err != nil {
		return nil, fmt.Errorf("预约信息修改失败")
	}

	return &pb.UpdateResponse{Message: "预约信息修改成功"}, nil
}
